// 找到 0 至 用户数据数字之间的所有质数
import React, {useState} from "react";

function isPrime(n) {
    const max = Math.ceil(Math.sqrt(n));
    if (n === 2) {
        return true;
    }

    for(let counter = 2; counter <= max; counter++) {
        if (n % counter === 0) {
            return false;
        }
    }

    return true;
}

function Demo() {
    const [selectedNum, setSelectedNum] = useState(100);
    const allPrimes = [];
    for (let counter = 2; counter < selectedNum; counter++) {
        if (isPrime(counter)) {
            allPrimes.push(counter)
        } 
    }

    return (
        <div className="border">
            <p className="title">Demo--useMemo--1</p>
            <form>
                <label htmlFor="num">Your number</label>
                <input 
                    type="number" 
                    value={selectedNum}
                    onChange={(event) => {
                        // 限制最大值为100k
                        let num = Math.min(100_100, Number(event.target.value));
                        setSelectedNum(num);
                    }}
                />
            </form>
            <p>
                There are {allPrimes.length} prime(s) between 1 and {selectedNum}:
                {' '}
                <span>
                    {allPrimes.join(', ')}
                </span>
            </p>
        </div>
    )
}

export default Demo;


